﻿namespace KingerRobot.MexicoSailunWMS.SpiService
{
    internal class BWmsBuildingMaterialNotInWare : IWmsBuildingMaterialNotInWare
    {
        public int AddNewBuildingMaterialNotInWare(BuildingMaterialNotInWareParam param)
        {
            var db = Db.Default;
            var result = 0;

            var checkType = param.CheckType;
            var materialCode = param.MaterialCode;

            var notInWare = db.WmsBuildingMaterialNotInWares.Where(t => t.MaterialCode == materialCode && t.CheckType == checkType).FirstOrDefault();

            if (notInWare is null)
            {
                result = db.WmsBuildingMaterialNotInWares.Insert(new PoWmsBuildingMaterialNotInWare()
                {
                    Id = XID.Nextval,
                    MaterialCode = materialCode,
                    CheckType = checkType,
                    CreateTime = DateTime.Now,
                    UpdateTime = DateTime.Now,
                });
            }

            return result;
        }

        public int DeleteNewBuildingMaterialNotInWare(long id)
        {
            var db = Db.Default;
            var result = db.WmsBuildingMaterialNotInWares.Where(t => t.Id == id).Delete();
            return result;
        }

        public List<PoWmsBuildingMaterialNotInWare> GetAllBuildingMaterialNotInWare()
        {
            var db = Db.Default;
            var result = db.WmsBuildingMaterialNotInWares.Where(t => t.CheckType == 2).ToList();
            return result;
        }
    }
}
